<template>
  <div>
    <el-row>
      <el-button
        icon="el-icon-refresh"
        type="primary"
        plain
        size="mini"
        @click="openData()"
        >刷新数据</el-button
      >
      <el-button
        icon="el-icon-circle-plus-outline"
        type="warning"
        plain
        size="mini"
        @click="onAdd()"
        :disabled="!zIsOpenData"
        >增加组织类型</el-button
      >
      <el-dialog
        v-model="FIsShowClassEdit"
        width="400PX"
        center
        destroy-on-close
      >
        <TFormCompanyClassEdit
          :zContextParams="contextParams"
          :zContextOnCall="onCallBack"
        ></TFormCompanyClassEdit>
      </el-dialog>
    </el-row>
    <el-table
      :data="FCompanyClassList"
      style="width: 100%; margin-top: 10px; margin-bottom: 20px"
      row-key="FClassID"
      border
      default-expand-all
      highlight-current-row
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      ref="myTable"
      :height="tableHeight"
    >
      <el-table-column prop="FClassName" label="类别"> </el-table-column>
      <el-table-column prop="FTreeCode" label="树形代码"> </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="primary"
            plain
            size="mini"
            @click="onEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            plain
            @click="onDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script  lang="ts">
import { Options, Vue } from "vue-class-component";
import TFormBase from "@/views/BaseForm";
import { TBaseCompanyClass } from "@/baselib/right/BaseRightClass";
import TFormCompanyClassEdit from "./CompanyClassEdit.vue";
//配合着用
@Options({
  name: "TFormCompanyClass",
  components: {
    TFormCompanyClassEdit,
  },
  computed: {},
  data() {},
})
export default class TFormCompanyClass extends TFormBase {
  declare $refs: {
    myTable: any;
  };
  private tableHeight: number = 200;
  private FIsShowClassEdit: boolean = false;
  private FIndexClass: TBaseCompanyClass = new TBaseCompanyClass();
  private FCompanyClassList: TBaseCompanyClass[] = [];
  private get contextParams() {
    return { IndexClass: this.FIndexClass, ClassList: this.FCompanyClassList };
  }
  public created() {
    this.openData();
  }
  //打开数据
  public async openData() {
    this.zIsOpenData = false;
    let lResult = await this.$myBaseRightAPI.getCompanyClassList();
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    this.FCompanyClassList = lResult.resultData;
    if (this.FCompanyClassList.length > 0) {
      if (this.$myStrHelper.stringIsEmpty(this.FCompanyClassList[0].FClassID)) {
        this.$myMsgHelp.msgDialog("返回的数据格式和前端的类定义不一至,请检查");
        return;
      }
    }
    this.zIsOpenData = true;
    this.$myMsgHelp.msgHintAutoClose(
      "获取数据成功",
      this.$myMsgHelp.msgSuccess
    );
  }
  //编辑界面回调列表事件
  private onCallBack(qAction: string, qClass: TBaseCompanyClass) {
    if (qAction == "close") {
      this.FIsShowClassEdit = false;
    }
    if (qAction == "save") {
      this.FIsShowClassEdit = false;
      //重新刷新数据
      this.openData();
    }
  }
  //增加结构类型
  private onAdd() {
    this.FIndexClass = new TBaseCompanyClass();
    this.FIsShowClassEdit = true;
  }
  private onEdit(qIndex: number, qClass: TBaseCompanyClass) {
    this.FIndexClass = qClass;
    this.FIsShowClassEdit = true;
  }
  private async onDelete(qIndex: number, qClass: TBaseCompanyClass) {
    if (this.$myStrHelper.stringIsEmptyYW(qClass.FClassID)) {
      this.$myMsgHelp.msgDialog("删除失败,无相关ID");
      return;
    }
    let lOK = await this.$myMsgHelp.msgDialogConfirm(
      "确定要删除结构类型【" + qClass.FClassName + "】"
    );
    if (!lOK) {
      return;
    }
    let lResult = await this.$myBaseRightAPI.delCompanyClass(qClass.FClassID);
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    this.$myMsgHelp.msgHintAutoClose("删除数据成功");
    this.openData();
  }
  public mounted() {
    //自适应列表高
    this.$nextTick(() => {
      let lHeight = window.innerHeight - this.$refs.myTable.$el.offsetTop - 120;
      this.tableHeight = lHeight;
    });
    window.addEventListener("resize", () => {
      this.$nextTick(() => {
        let lHeight =
          window.innerHeight - this.$refs.myTable.$el.offsetTop - 120;
        this.tableHeight = lHeight;
      });
    });
  }
}
</script>